package com.jhd.fuzhou.view.daily;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.github.barteksc.pdfviewer.PDFView;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.EPrinterService;
import com.jhd.fuzhou.service.PrinterService;
import com.jhd.fuzhou.util.DownloadStreamThread;
import com.jhd.fuzhou.util.UploadFile;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.view.login.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jhd.fuzhou.view.login.MainActivity.supervisionInspectionVo;

public class DocumentReprinterActivity extends BaseActivity {

    private String url,name,unitName;//文书的下载路径,文书名称，监督单位名称
    private ProgressDialog _dialog;
    private Context context=this;
    @BindView(R.id.pdfView)PDFView pdfView;
    @BindView(R.id.btn_print)Button btn_print;
    @BindView(R.id.btn_download)Button btn_download;
    private PrinterService printerService;
    private EPrinterService ePrinterService;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_document_reprinter);
    }

    @Override
    protected void processLogic() {
        onBack();
        if(UtilHelper.ePrinterType==0){
            printerService = new PrinterService.Builder(this).build();
        }else{
            ePrinterService=new EPrinterService.Builder(this).build();
        }
        url=getIntent().getStringExtra("url");
        name=getIntent().getStringExtra("name");
        unitName=getIntent().getStringExtra("unitName");
        _dialog = new ProgressDialog(context);
        _dialog.setMessage("正在获取文书,请稍等...");
        _dialog.show();
        new DownloadStreamThread(url,getExternalFilesDir(null).getPath() + "/Download/document.pdf",_dialog,context,pdfView).start();
    }

    @OnClick({R.id.btn_download,R.id.btn_print})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_download:
                UploadFile.copyFile(getExternalFilesDir(null).getPath() + "/Download/document.pdf",
                        getExternalFilesDir(null).getPath() + "/Download/RePrinter/", (unitName==null?"":unitName)+name+".pdf",context);
                break;
            case R.id.btn_print:
                if(UtilHelper.ePrinterType==0) {
                    printerService.executePrint(btn_print);
                }else {
                    ePrinterService.executePrint(btn_print);
                }
                break;
        }
    }
}